#include <ExtrasState.h>

template<> ExtrasState* Ogre::Singleton<ExtrasState>::ms_Singleton = 0;

ExtrasState::ExtrasState()
{
  _extraType = Records;
}

void ExtrasState::enter()
{
  _root = Ogre::Root::getSingletonPtr();
  _win = _root->getAutoCreatedWindow();
  _overlayMgr = Ogre::OverlayManager::getSingletonPtr();
  _exitGame = false;

  if (_extraType == Records)
  {
    Ogre::OverlayElement* oe = _overlayMgr->getOverlayElement("recordsText");
    oe->setLeft(567 * _win->getWidth() / 1280);
    oe->setTop(199 * _win->getHeight() / 800);

    std::stringstream s;
    std::list<unsigned int>* records = IntroState::getSingletonPtr()->getRecords();
    std::list<unsigned int>::iterator it;
    for (it = records->begin(); it != records->end(); ++it)
      s << Ogre::StringConverter::toString(*it) << std::endl;

    oe->setCaption(s.str());
    _overlayMgr->getOverlayElement("Records")->show();
  }

  else
    _overlayMgr->getOverlayElement("Credits")->show();
}

void ExtrasState::exit()
{
  if (_extraType == Records)
    _overlayMgr->getOverlayElement("Records")->hide();
  else
    _overlayMgr->getOverlayElement("Credits")->hide();

  _overlayMgr->getOverlayElement("menuImage")->show();
}

void ExtrasState::pause() {}

void ExtrasState::resume() {}

bool ExtrasState::frameStarted(const Ogre::FrameEvent& evt)
{
  return true;
}

bool ExtrasState::frameEnded(const Ogre::FrameEvent& evt)
{
  return not _exitGame;
}

void ExtrasState::setType(const ExtraType e)
{
  _extraType = e;
}

void ExtrasState::keyPressed(const OIS::KeyEvent &e)
{
  popState();
}

void ExtrasState::keyReleased(const OIS::KeyEvent &e) {}

void ExtrasState::mouseMoved(const OIS::MouseEvent &e) {}

void ExtrasState::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
{
  popState();
}

void ExtrasState::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id) {}

ExtrasState* ExtrasState::getSingletonPtr()
{
  return ms_Singleton;
}

ExtrasState& ExtrasState::getSingleton()
{ 
  assert(ms_Singleton);
  return *ms_Singleton;
}
